package com.haierp.service.youzan.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.OuterOrderDetailMapper;
import com.haierp.model.ErpOrder;
import com.haierp.model.sale.OuterOrder;
import com.haierp.model.sale.OuterOrderDetail;
import com.haierp.service.IErpOrderService;
import com.haierp.service.IOuterOrderService;
import com.haierp.service.youzan.IYzService;
import com.haierp.util.DimensionCodeUtil;
import com.youzan.open.sdk.client.auth.Token;
import com.youzan.open.sdk.client.core.DefaultYZClient;
import com.youzan.open.sdk.client.core.YZClient;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemCreate;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemGet;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemSkuUpdate;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemUpdate;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemUpdateDelisting;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemUpdateListing;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanLogisticsExpressGet;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanLogisticsOnlineConfirm;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanMaterialsStoragePlatformImgUpload;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemCreateParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemCreateResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemGetResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemSkuUpdateParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemSkuUpdateResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateDelistingParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateDelistingResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateListingParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateListingResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanLogisticsExpressGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanLogisticsExpressGetResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanLogisticsExpressGetResult.LogisticsExpressOpenApiModel;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanLogisticsOnlineConfirmParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanLogisticsOnlineConfirmResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanMaterialsStoragePlatformImgUploadParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanMaterialsStoragePlatformImgUploadResult;

public class YzServiceImpl implements IYzService {
	@Autowired
	private IOuterOrderService outerOrderService;
	@Autowired
	private IErpOrderService erpOrderService;
	@Autowired
	private OuterOrderDetailMapper outerOrderDetailMapper;
	
	private YZClient dkcClient = null;
	
	public YzServiceImpl(String clientId, String clientSecret, String kdtId) {
		if(StringUtil.isBlank(DimensionCodeUtil.youZanToken)) {
			DimensionCodeUtil.setClientKey(clientId, clientSecret, kdtId);
			DimensionCodeUtil.getYouZanToken();
		}
		dkcClient =  new DefaultYZClient(new Token(DimensionCodeUtil.youZanToken));//免签名方式
	}
	
	//添加商品
	public YouzanItemCreateResult youzanItemCreate(YouzanItemCreateParams youzanItemCreateParams){
		YouzanItemCreate youzanItemCreate = new YouzanItemCreate();
		youzanItemCreate.setAPIParams(youzanItemCreateParams);
		YouzanItemCreateResult result = dkcClient.invoke(youzanItemCreate);
		return result;
	}
	
	//更新商品
	public YouzanItemUpdateResult youzanItemUpdate(YouzanItemUpdateParams youzanItemUpdateParams){
		YouzanItemUpdate youzanItemUpdate = new YouzanItemUpdate();
		youzanItemUpdate.setAPIParams(youzanItemUpdateParams);
		YouzanItemUpdateResult result = dkcClient.invoke(youzanItemUpdate);
		return result;
	}
	//下架
	@Override
	public YouzanItemUpdateDelistingResult youzanItemUpdateDelisting(YouzanItemUpdateDelistingParams youzanItemUpdateDelistingParams) {
		YouzanItemUpdateDelisting youzanItemUpdateDelisting = new YouzanItemUpdateDelisting();
		youzanItemUpdateDelisting.setAPIParams(youzanItemUpdateDelistingParams);
		YouzanItemUpdateDelistingResult result = dkcClient.invoke(youzanItemUpdateDelisting);
		return result;
	}
	//上架
	@Override
	public YouzanItemUpdateListingResult youzanItemUpdateListing(YouzanItemUpdateListingParams youzanItemUpdateListingParams) {
		YouzanItemUpdateListing youzanItemUpdateListing = new YouzanItemUpdateListing();
		youzanItemUpdateListing.setAPIParams(youzanItemUpdateListingParams);
		YouzanItemUpdateListingResult result = dkcClient.invoke(youzanItemUpdateListing);
		return result;
	}
	//更新sku信息(例如：库存)
	@Override
	public YouzanItemSkuUpdateResult youzanItemSkuUpdate(YouzanItemSkuUpdateParams youzanItemSkuUpdateParams) {
		YouzanItemSkuUpdate youzanItemSkuUpdate = new YouzanItemSkuUpdate();
		youzanItemSkuUpdate.setAPIParams(youzanItemSkuUpdateParams);
		YouzanItemSkuUpdateResult result = dkcClient.invoke(youzanItemSkuUpdate);
		return result;
	}
	//获取单个商品信息
	@Override
	public YouzanItemGetResult youzanItemGet(YouzanItemGetParams youzanItemGetParams) {		
		YouzanItemGet youzanItemGet = new YouzanItemGet();
		youzanItemGet.setAPIParams(youzanItemGetParams);
		YouzanItemGetResult result = dkcClient.invoke(youzanItemGet);
		return result;
	}

	@Override
	public YouzanMaterialsStoragePlatformImgUploadResult youzanMaterialsStoragePlatformImgUpload(
		YouzanMaterialsStoragePlatformImgUploadParams youzanMaterialsStoragePlatformImgUploadParams) {
		YouzanMaterialsStoragePlatformImgUpload youzanMaterialsStoragePlatformImgUpload = new YouzanMaterialsStoragePlatformImgUpload();
		youzanMaterialsStoragePlatformImgUpload.setAPIParams(youzanMaterialsStoragePlatformImgUploadParams);
		YouzanMaterialsStoragePlatformImgUploadResult result = dkcClient.invoke(youzanMaterialsStoragePlatformImgUpload);
		return result;
	}

	@Override
	public YouzanLogisticsOnlineConfirmResult youzanSendLogisticsOnline(String sid,String outSid,String logisticscompany) {
		YouzanLogisticsOnlineConfirmParams youzanLogisticsOnlineConfirmParams = new YouzanLogisticsOnlineConfirmParams();
    	youzanLogisticsOnlineConfirmParams.setTid(sid);//交易编号
    	youzanLogisticsOnlineConfirmParams.setIsNoExpress(0L);
    	youzanLogisticsOnlineConfirmParams.setOutStype(logisticscompany);
    	youzanLogisticsOnlineConfirmParams.setOutSid(outSid);
    	YouzanLogisticsOnlineConfirm youzanLogisticsOnlineConfirm = new YouzanLogisticsOnlineConfirm();
    	youzanLogisticsOnlineConfirm.setAPIParams(youzanLogisticsOnlineConfirmParams);
    	YouzanLogisticsOnlineConfirmResult  result = dkcClient.invoke(youzanLogisticsOnlineConfirm);
		return result;
	}

	@Override
	public Map<String, String>  youzanGetLogisticsCompany() {
		YouzanLogisticsExpressGetParams youzanLogisticsExpressGetParams = new YouzanLogisticsExpressGetParams();
		YouzanLogisticsExpressGet youzanLogisticsExpressGet = new YouzanLogisticsExpressGet();
		youzanLogisticsExpressGet.setAPIParams(youzanLogisticsExpressGetParams);
		YouzanLogisticsExpressGetResult result = dkcClient.invoke(youzanLogisticsExpressGet);
		LogisticsExpressOpenApiModel[] logisticsExpressOpenApiModel= result.getAllExpress();
		Map<String, String> LogisticsExpressKey = new HashMap<String, String>();
		for(int j=0; j<logisticsExpressOpenApiModel.length; j++) {
			LogisticsExpressOpenApiModel  model = logisticsExpressOpenApiModel[j];
			LogisticsExpressKey.put(model.getName(), String.valueOf(model.getId()));
		}
		return LogisticsExpressKey;
	}
}
